package com.ext

import com.wugui.datatx.core.util.DateUtil
import com.wugui.datax.admin.tool.FTPTools
import groovy.sql.Sql
import org.apache.commons.lang3.StringUtils

/**
 * @author dxy* @date 2020/12/15 14:10
 */
class ToTxt {
    private def dbType;
    private def filePath="/opt/pmo/dataxweb";
    private def ftpHost="15.22.0.175";
    private def ftpPort=21
    private def ftpUseName="ftpUser"
    private def ftpPassword="111qqq@@@"
    private def ftpPath="syzj"
    private def dateField = null
    private def dateRange = null

    public ToTxt(dbType) {
        this.dbType = dbType;
    }


    def apply(String host, Integer port, String schema, String username, String password, String table, String dateField, String dateRange) {
        def sqlConn = null;
        if(dbType == "oracle") {
            sqlConn = Sql.newInstance("jdbc:oracle:thin:@${host}:${port}:${schema}",
                    username, password, "oracle.jdbc.driver.OracleDriver")
        }else if(dbType == "mysql") {
            sqlConn = Sql.newInstance("jdbc:mysql://${host}:${port}/${schema}",
                    username, password, "com.mysql.jdbc.Driver")
        }

        if(sqlConn == null) {
            return "dbType:${dbType} not support!!!"
        }

        def sqlStr = "select * from " + table
        if(dateField && dateRange) {
            def days = []
            for(d in dateRange.split(",")) {
                String day = DateUtil.format(DateUtil.parse(d.trim(), "yyyyMMdd"), "yyyy-MM-dd")
                days << dateField + " like ('" + day + " %')"
            }
            sqlStr = sqlStr + " where " + days.join(" or ")
        }

        println "#########SQL:" + sqlStr
        String fileName = buildFileName(host, port, schema, table);
        def file = new File(filePath + "/" + fileName)
        int c =  0
        sqlConn.eachRow(sqlStr) {row->
            def vals = []
            row.toRowResult().keySet().each {k->
                if(!k.toString().equalsIgnoreCase(dateField)) {
                    vals << (row[k] == null ? "" : row[k])
                }
            }
            file.append(vals.join("&") + "&\r\n")
            c++
        }

        println "total:" + c + ",file:" + file.getName() + " generator success!!!!!!!!"
        if(c > 0) {
            String ftpSaveName = table + "+" + DateUtil.format(new Date(), "yyyyMMdd") + ".txt"
            println "==========================upload to ftp start==============================="
            println "ftpHost:" + ftpHost
            println "ftpPort:" + ftpPort
            println "ftpUseName:" + ftpUseName
            println "ftpPassword:" + ftpPassword
            println "ftpSaveName:" + ftpSaveName
            FTPTools.upload(ftpHost, ftpPort, ftpUseName, ftpPassword, ftpPath, new FileInputStream(file), ftpSaveName)
            println "==========================upload to ftp success==============================="
        }

        return true
    }

    private buildFileName(host, port, schema, table) {
        return host + "-" + port + "-" + schema + "-" + table + "+" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".txt"
    }

    ToTxt setFilePath(filePath) {
        this.filePath = filePath
        return this
    }

    ToTxt setFtpHost(ftpHost) {
        this.ftpHost = ftpHost
        return this
    }

    ToTxt setFtpPort(ftpPort) {
        this.ftpPort = ftpPort
        return this
    }

    ToTxt setFtpUseName(ftpUseName) {
        this.ftpUseName = ftpUseName
        return this
    }

    ToTxt setFtpPassword(ftpPassword) {
        this.ftpPassword = ftpPassword
        return this
    }

    ToTxt setFtpPath(ftpPath) {
        this.ftpPath = ftpPath
        return this
    }
}
